package com.substation.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by admin on 2017/12/25.
 */

public class DateUtils {

    public static String getNowHours(){
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("HH:mm");
        long now= System.currentTimeMillis();
        return simpleDateFormat.format(new Date(now));
    }
    public static boolean compareHours(String startHour, String endHour){
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("HH:mm");
        try {
            Date startD=simpleDateFormat.parse(startHour);
            Date endD=simpleDateFormat.parse(endHour);
            return startD.getTime()<endD.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return false;
    }
    public static String LongToString(long time){
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy/MM/dd HH:mm");
        return simpleDateFormat.format(new Date(time));
    }
    public static String getNewTime(){
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return simpleDateFormat.format(new Date());
    }
    public static String getDateToString(Date date){
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return simpleDateFormat.format(date);
    }
    public static String getDateToStringNoHH(Date date){
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
        return simpleDateFormat.format(date);
    }
    public static Date getStringToDateNoHH(String date){
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
        try {
            return simpleDateFormat.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return new Date();
    }
    public static String LongToStringMMDD(long time){
        SimpleDateFormat simpleDateFormat1=new SimpleDateFormat("MM月dd日");
        return simpleDateFormat1.format(new Date(time));
    }
    public static Long StringTOLongyyMMdd(String time){
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy/MM/dd");
        try {
            return simpleDateFormat.parse(time).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0l;
    }
    public static Long StringToLongHHmm(String time){
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("HH:mm");
        try {
            return simpleDateFormat.parse(time).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0l;
    }
    public static String LongToStringyyMMdd(long time){
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy/MM/dd");
        return simpleDateFormat.format(new Date(time));
    }
    public static String DateToString(Date date){
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        return simpleDateFormat.format(date);
    }
    public static String timeTotimeMMDD(String datetime){
        SimpleDateFormat simpleDateFormat1=new SimpleDateFormat("yyyy/MM/dd");
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("MM月dd日");
        try {
            return simpleDateFormat.format(simpleDateFormat1.parse(datetime));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return "";
    }

    public static String dateToWeek(String datetime) {
        SimpleDateFormat f = new SimpleDateFormat("yyyy/MM/dd");
        String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
        Calendar cal = Calendar.getInstance(); // 获得一个日历
        Date datet = null;
        try {
            datet = f.parse(datetime);
            cal.setTime(datet);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1; // 指示一个星期中的某天。
        if (w < 0)
            w = 0;
        return weekDays[w];
    }
}
